package com.cyber.range.diffinfo.impl;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.cyber.range.diffinfo.GetDiffInfoService;
import com.cyber.range.diffinfo.module.diff.DiffInfoQuery;
import com.cyber.range.diffinfo.module.diff.DiffJavaFile;
import com.cyber.range.diffinfo.module.diff.DiffLine;
import com.cyber.range.diffinfo.module.diff.DiffResult;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;


/**
 * @author kuohai
 */
@Service
public class GetDiffInfoServiceImpl implements GetDiffInfoService {
    public static final Logger logger = LoggerFactory.getLogger(GetDiffInfoServiceImpl.class);

    @Override
    public DiffResult getDiffInfo() {
        DiffJavaFile diffJavaFile = new DiffJavaFile();
        List<DiffJavaFile> diffJavaFilesList = new ArrayList<DiffJavaFile>();
        DiffResult diffResult = new DiffResult();

        String diffStr = "diff --git a/src/main/java/com/cyber/range/controller/DemoController.java b/src/main/java/com/cyber/range/controller/DemoController.java\n" +
                "index cf5021d..f38b14e 100644\n" +
                "--- a/src/main/java/com/cyber/range/controller/DemoController.java\n" +
                "+++ b/src/main/java/com/cyber/range/controller/DemoController.java\n" +
                "@@ -19,10 +19,12 @@ public class DemoController {\n" +
                "         }\n" +
                "         if (param.getMethod().equals(\"add\")) {\n" +
                "             result = param.getP1() + param.getP2();\n" +
                "+            System.out.println(01);\n" +
                "         } else if (param.getMethod().equals(\"sub\")) {\n" +
                "             result = param.getP1() - param.getP2();\n" +
                "         } else if (param.getMethod().equals(\"mul\")) {\n" +
                "             result = param.getP1() * param.getP2();\n" +
                "+            System.out.println(02);\n" +
                "         } else if (param.getMethod().equals(\"div\")) {\n" +
                "             result = param.getP1() / param.getP2();\n" +
                "         } else {\n";

        logger.info(diffStr);
        String[] diffLines = diffStr.split("\n");
        int startLineNum;
        int lineNum = 0;
        List<DiffLine> diffLinesesList = new ArrayList<DiffLine>();
        List<Integer> diffLineNumsList = new ArrayList<Integer>();
        String filepath="";
        //循环代码行
        for (String line : diffLines) {
            DiffLine diffLine = new DiffLine();
            if (line.startsWith("---")&&!line.endsWith(".java")) {
                break;
                //差异小结头情况
            } else if (line.startsWith("---") || line.startsWith("+++")) {
                filepath = line.replace("--- a/src/main/java/","").replace("+++ b/src/main/java/","");

                continue;
                //行范围标识情况
            } else if (line.replace("\t", "").startsWith("@@")) {
                String startLineNumStr = StringUtils.substringBetween(line, "+", ",");
                startLineNum = Integer.parseInt(startLineNumStr);
                lineNum = startLineNum;
                //行头减号的情况
            } else if (line.replace("\t", "").startsWith("-")) {
                continue;
                //行头加号的情况
            } else if (line.replace("\t", "").startsWith("+")) {
                diffLine.setIndex(lineNum);
                diffLine.setLine(line.replace("\t", "").replace("+", "").replace("-", ""));
                diffLinesesList.add(diffLine);
                diffLineNumsList.add(lineNum);
                lineNum++;
                //除减号及减号的情况
            } else {
                lineNum++;
                continue;
            }
            diffJavaFile.setDiff_line_nums(diffLineNumsList);
            diffJavaFile.setDiff_lines(diffLinesesList);
        }
        diffJavaFile.setJava_file(filepath);
        diffJavaFilesList.add(diffJavaFile);
        diffResult.setDiff_java_files(diffJavaFilesList);
        return diffResult;
    }

}

